package com.tac.common.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


public class ImgUtis {

    //一次只能接收一个文件用来传头像

   public static String saveimg(MultipartFile file,String s){
       String originalFilename = file.getOriginalFilename();
       String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
       String imgName=UUID.randomUUID().toString()+suffix;
       s=  s.replace("\\","\\\\");
       try {
           file.transferTo(new File(s+imgName));
       } catch (IOException e) {
           e.printStackTrace();
       }
       return "/file/"+imgName;
   }
   //一次可以接收多个文件用来填好商品详情图片
    public static String saveimgs(List<MultipartFile> files,String s) throws IOException {
       String names="";
       for(MultipartFile multipartFile:files){
           String originalFilename = multipartFile.getOriginalFilename();
           String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
           String imgname=UUID.randomUUID().toString()+suffix;
           names=names+"/file/"+imgname+",";
           s=  s.replace("\\","\\\\");
           multipartFile.transferTo(new File(s+imgname));
       }
       String names2=names.substring(0,names.length()-1);
       return names2;
    }

}

